import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    // 用户基本信息
    userInfo: {
      id: '',
      name: '学生用户',
      phone: '138****8888',
      avatar: '/static/logo.png',
      verified: false,
      realName: '',
      idCard: '',
      school: '',
      degree: '',
      major: '',
      role: 'student', // 用户角色：student(学生) 或 teacher(教师)
      researchExperience: '', // 科研经历
      personalStatement: '', // 个人自述
      idCardFront: '', // 身份证人像面
      idCardBack: '', // 身份证国徽面
      educationCert: '', // 学历证件
      tutorSkills: [], // 擅长的辅导技能
      skillDescription: '' // 技能详细说明
    },
    
    // 认证信息
    authInfo: {
      status: 'none', // none, pending, verified, rejected
      submitTime: '',
      rejectReason: ''
    },
    
    // 钱包信息
    walletInfo: {
      balance: '0.00',
      totalSpent: '0.00',
      orderCount: 0,
      totalRecharge: '0.00'
    },
    
    // 登录状态
    isLoggedIn: false,
    token: ''
  }),
  
  getters: {
    // 获取用户显示名称
    displayName: (state) => {
      return state.userInfo.realName || state.userInfo.name || '用户'
    },
    
    // 获取认证状态文本
    authStatusText: (state) => {
      const statusMap = {
        none: '未认证',
        pending: '审核中',
        verified: '已认证',
        rejected: '认证失败'
      }
      return statusMap[state.authInfo.status] || '未知状态'
    },
    
    // 是否已认证
    isVerified: (state) => {
      return state.authInfo.status === 'verified'
    }
  },
  
  actions: {
    // 设置用户信息
    setUserInfo(userInfo) {
      this.userInfo = { ...this.userInfo, ...userInfo }
    },
    
    // 设置认证信息
    setAuthInfo(authInfo) {
      this.authInfo = { ...this.authInfo, ...authInfo }
    },
    
    // 设置钱包信息
    setWalletInfo(walletInfo) {
      this.walletInfo = { ...this.walletInfo, ...walletInfo }
    },
    
    // 登录
    login(token, userInfo) {
      this.token = token
      this.isLoggedIn = true
      if (userInfo) {
        this.setUserInfo(userInfo)
      }
      // 保存到本地存储
      uni.setStorageSync('token', token)
      uni.setStorageSync('userInfo', this.userInfo)
    },
    
    // 登出
    logout() {
      this.token = ''
      this.isLoggedIn = false
      this.userInfo = {
        id: '',
        name: '学生用户',
        phone: '',
        avatar: '/static/logo.png',
        verified: false,
        realName: '',
        idCard: '',
        school: '',
        degree: '',
        major: ''
      }
      this.authInfo = {
        status: 'none',
        submitTime: '',
        rejectReason: ''
      }
      this.walletInfo = {
        balance: '0.00',
        totalSpent: '0.00',
        orderCount: 0,
        totalRecharge: '0.00'
      }
      // 清除本地存储
      uni.removeStorageSync('token')
      uni.removeStorageSync('userInfo')
    },
    
    // 从本地存储恢复用户信息
    restoreUserInfo() {
      const token = uni.getStorageSync('token')
      const userInfo = uni.getStorageSync('userInfo')
      
      if (token) {
        this.token = token
        this.isLoggedIn = true
      }
      
      if (userInfo) {
        this.setUserInfo(userInfo)
      }
    },
    
    // 更新用户头像
    updateAvatar(avatar) {
      this.userInfo.avatar = avatar
      uni.setStorageSync('userInfo', this.userInfo)
    },
    
    // 提交认证信息
    submitAuth(authData) {
      this.setUserInfo(authData)
      this.authInfo.status = 'pending'
      this.authInfo.submitTime = new Date().toLocaleString()
      uni.setStorageSync('userInfo', this.userInfo)
    }
  }
})
